package com.zkingsoft.web.action;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.WebTemplate;
import com.zkingsoft.web.dao.WebTemplateDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @description 网页模板
 * @author 姜友瑶
 * @date 2018-12-26 18:59
 */
@Controller
@RequestMapping(value = "admin/webTemplate")
public class WebTemplateAction {

	@Autowired
	private WebTemplateDao webTemplateDao;
	
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="WebTemplate_BEV";

	/**
	 * 查询当前公司所有的模板
	 */
	@RequestMapping(value =  "/showAll")
	public @ResponseBody AjaxResult showAll(WebTemplate webTemplate) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webTemplate.setCompanyId(user.getCompanyId());
		List<WebTemplate> dataList = webTemplateDao.selectByModel(webTemplate);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList);
		return result;
	}



	/**
	 * 列表显示
	 */
	@RequestMapping(value =  "/showList")
	public @ResponseBody AjaxResult showList(WebTemplate webTemplate, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webTemplate.setCompanyId(user.getCompanyId());
		List<WebTemplate> dataList = webTemplateDao.selectInPage(webTemplate, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
				webTemplateDao.selectTotalRecord(webTemplate));
		return result;
	}
   	
	/**
	 * 新增
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value =  "/addWebTemplate")
	public @ResponseBody AjaxResult addWebTemplate(WebTemplate webTemplate) {
	    SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webTemplate.setId(UUIDUtil.getRandomID());
		webTemplate.setCreateBy(user.getSuName());
		webTemplate.setUpdateBy(user.getSuName());
		webTemplate.setCompanyId(user.getCompanyId());
	 	int i=webTemplateDao.insert(webTemplate);
	 	if(i > 0){
	 		return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "网页模板");
	 	}else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}
	
	
	
	
	
	/**
	 * 修改
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value =  "/modifyWebTemplate")
	public @ResponseBody AjaxResult modifyWebTemplate(WebTemplate newWebTemplate) {
	   	WebTemplate oldWebTemplate = WebUtil.getSessionAttribute(BEV);
		int i = 0;
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldWebTemplate, newWebTemplate)) {
				i = MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldWebTemplate, newWebTemplate);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newWebTemplate);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("id", oldWebTemplate.getId());
			webTemplateDao.updateByMap(modifyMap);
		}
		i = MatrixConstance.DML_SUCCESSS;
		WebUtil.removeSessionAttribute(BEV);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "网页模板");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
		}
	}
	
	
	
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value =  "/editForm")
	public String editForm(String id) {
		WebTemplate webTemplate;
		if (id != null) {
			webTemplate = webTemplateDao.selectById(id);
			WebUtil.getRequest().setAttribute("obj", webTemplate);
			WebUtil.setSessionAttribute(BEV, webTemplate);
		}
		return "admin/web/WebTemplate-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<String> ids = StringUtils.strToCollToString(keys, ",");
		int i =  webTemplateDao.deleteByIds(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
		} else {
			throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
		}
	}
  
}